package com.bilibili.bilibili.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.VolleyError;
import com.bilibili.bilibili.R;
import com.bilibili.bilibili.adapter.DiscoverRecyclerViewAdapter;
import com.bilibili.bilibili.model.DiscoverAuthorityBean;
import com.bilibili.bilibili.network.HttpGetData;
import com.bilibili.bilibili.network.VolleyInterface;
import com.bilibili.bilibili.utils.GsonUtil;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import static android.content.ContentValues.TAG;


/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverFragment extends Fragment {


    private View inflate;
    private DiscoverRecyclerViewAdapter discoverRecyclerViewAdapter;
    private RecyclerView recyclerView;
    //private List<String> strings;


    public DiscoverFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflate = inflater.inflate(R.layout.fragment_discover, container, false);


        return inflate;
    }

    private void initDabase() {

        String path="http://s.search.bilibili.com/main/hotword?access_key=ec0f54fc369d8c104ee1068672975d6a&actionKey=appkey&appkey=27eb53fc9058f8c3";
        HttpGetData.RequestGet(getActivity(), path, "discover", new VolleyInterface(getActivity(),VolleyInterface.mListener,VolleyInterface.mErrorListener) {
            @Override
            public void onMySuccess(String result) {
                DiscoverAuthorityBean discoverAuthorityBean = GsonUtil.parseJsonToObj(result, new TypeToken<DiscoverAuthorityBean>() {
                });
                 List<DiscoverAuthorityBean.ListBean>  list = discoverAuthorityBean.getList();
                discoverRecyclerViewAdapter = new DiscoverRecyclerViewAdapter(getActivity(), list);
                recyclerView.setAdapter(discoverRecyclerViewAdapter);
              /*  new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message obtain = Message.obtain();
                        obtain.obj=list;
                        handler.sendMessage(obtain);
                    }
                });*/

            }

            @Override
            public void onMyError(VolleyError error) {
                Log.d(TAG, "onMyError: ");

            }
        });


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initDabase();
        recyclerView = (RecyclerView) inflate.findViewById(R.id.discover_recycler_view);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));


    }
   /* Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            List<DiscoverAuthorityBean.ListBean>=msg.obj;
        }
    };*/
}
